/**
 * This file is a part of qloudgen-sec. 
 * You can redistribute qloudgen-sec and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-sec is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.sec.cas.client.session;

import javax.servlet.http.HttpSession;

import org.jasig.cas.client.session.SessionMappingStorage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.qloudgen.sec.cache.Cache;
import org.qloudgen.sec.cas.client.CacheStorage;

/**
 * <p>Title: Session Mapping Storage Implementation</p>
 * <p>Description: Wrappered cas ticket and session storage.</p>
 * <p>Copyright: qloudgen/sec 2014</p>
 * @author <a href="mailto:hyq.dd.sun@gmail.com">glue.3M</a>
 */
public final class CasSessionMappingStorage extends CacheStorage implements SessionMappingStorage {
	/**
	 * Log output.
	 */
	private final static Logger logger = LoggerFactory.getLogger( CasSessionMappingStorage.class );
	/**
	 *
	 */
	private final static String DEFAULT_SESSION_CACHE = "CAS-SESSION";
	
	/**
	 * Default constructor.
	 */
	public CasSessionMappingStorage() {
		super( DEFAULT_SESSION_CACHE , null );
	}
	
	/**
	 * Add a session by its mapping id.
	 *
	 * @param mappingId
	 * @param session
	 */
	public void addSessionById(final String mappingId , final HttpSession session) {
		/**
		 * Do put session id & mapping id.
		 */
		if ( session.getId() != null ) {
			getCache().put( session.getId() , mappingId );
			logger.debug( "Started add session id by mapping id...[{},{}]" , session.getId() , mappingId );
		}
		/**
		 * Do put mapping id & session.
		 */
		final HttpSession wrapperedSession = new HttpSessionWrapper( session.getId() );
		if ( mappingId != null ) {
			getCache().put( mappingId , wrapperedSession );
			logger.debug( "Started add mapping id by session...[{},{}]" , mappingId , wrapperedSession );
			logger.debug( "Started add mapping id by session...[{},{}]" , mappingId , session );
		}
	}
	
	/**
	 * Remove a session by its id.
	 *
	 * @param sessionId
	 */
	public void removeBySessionById(final String sessionId) {
		/**
		 * Do get mapping id.
		 */
		final String mappingId = ( String )( getCache().get( sessionId ) );
		/**
		 * Do remove mapping id & session
		 */
		if ( mappingId != null ) {
			getCache().remove( mappingId );
			logger.debug( "Started remove session by mapping id...{}" , mappingId );
		}
		/**
		 * Do remove session id & mapping id.
		 */
		if ( sessionId != null ) {
			getCache().remove( sessionId );
			logger.debug( "Started remove mapping id by session id...{}" , sessionId );
		}
	}
	
	/**
	 * Remove a session by its mapping id.
	 * 
	 * @param mappingId
	 * @return HttpSession
	 */
	public HttpSession removeSessionByMappingId(final String mappingId) {
		final HttpSession session = ( HttpSession )( getCache().get( mappingId ) );
		if ( session != null ) {
			removeBySessionById( session.getId() );
		}
		//
		logger.debug( "Current removed session is...{}" , session );
		return session;
	}
	
	/**
	 * Returns the String representation.
	 *
	 * @return String
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append( "[" );
		builder.append( super.toString() );
		builder.append( "]" );
		//
		return builder.toString();
	}
}